package concurrent.util;

import java.util.concurrent.Phaser;

/**
 * User: Administrator
 * Date: 2020/12/1
 * Time: 15:44
 * Description:
 * Java 7的并发包中推出了Phaser，其功能跟CyclicBarrier和CountDownLatch有些重叠，但是提供了更灵活的用法，例如支持动态调整注册任务的数量等
 */
public class PhaserDemo1 {
    public static void main(String[] args) {
        Phaser phaser = new Phaser();

        for (int i = 0; i < 10; i++) {
            new Thread(()->{
                int register = phaser.register();
                System.out.println(Thread.currentThread().getName() + "register:" + register);
                try {
                    Thread.sleep(1000);
                    int arrive = phaser.arrive();
                    System.out.println(Thread.currentThread().getName() + "arrive后执行:" + arrive);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();
        }
        int registeredParties = phaser.getRegisteredParties();
        System.out.println("已注册parties:" + registeredParties);
        int register = phaser.register();
        System.out.println("主线程 register:" + register);
        int arrive = phaser.arriveAndAwaitAdvance();
        System.out.println("主线程 arrive:" + arrive);
    }
}
